package com.example.ticketingserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ticketingserver.dto.R;
import com.example.ticketingserver.entity.Favorite;
import com.example.ticketingserver.entity.Favoritedetails;
import com.example.ticketingserver.entity.Station;
import com.example.ticketingserver.entity.User;
import com.example.ticketingserver.service.FavoriteService;
import com.example.ticketingserver.service.FavoritedetailsService;
import com.example.ticketingserver.service.StationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static cn.hutool.poi.excel.sax.ElementName.f;
import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;


@Tag(name = "收藏夹详情管理",description = "收藏夹和车站关联信息管理，收藏夹详情添加，删除，查询")
@CrossOrigin
@RestController
public class FavoritedetailsController {
    @Autowired
    private FavoritedetailsService favoritedetailsService;
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private StationService stationService;


    @Operation(summary = "添加到收藏夹",description = "将收藏夹和车站关联")
    @Parameters({
            @Parameter(name = "listId",description = "收藏夹id",required = true),
            @Parameter(name = "sid",description = "车站id" ,required = true)
    })
    @PostMapping("/favoritedetails/add")
    public R addListdetails(@RequestParam Integer listId, @RequestParam Integer sid){
        QueryWrapper<Favoritedetails> query = Wrappers.query(Favoritedetails.class);
        //select * from listdetails where listid=? & mid=?
        query.eq("listid",listId)
                .eq("sid",sid);
        Favoritedetails one = favoritedetailsService.getOne(query);
        if(Objects.nonNull(one)){
            return R.fail().setMsg("该车站已经在收藏夹中存在");
        }
        Favoritedetails favoritedetails = new Favoritedetails();
        favoritedetails.setListid(listId);
        favoritedetails.setSid(sid);
        if(favoritedetailsService.save(favoritedetails)){
            return R.ok().setMsg("成功加入收藏夹");
        }
        return R.fail().setMsg("加入收藏夹失败");
    }
    @Operation(summary = "查询所有收藏夹车站",description = "查询所有的该收藏夹的车站")
    @Parameters({
            @Parameter(name = "page",description = "当前页码",required = true),
            @Parameter(name = "limit",description = "每页的条数",required = true),
    })
    @GetMapping("/favoritedetails/listAll")
    public R queryAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer limit){
        List<Favoritedetails> list = favoritedetailsService.list(new Page<Favoritedetails>(page,limit));
        list.forEach(fd->{
//            //获取收藏夹id
//            Integer listid = fd.getListid();
//            Favorite favorite = favoriteService.getById(listid);
//            fd.setFavorite(favorite);
//            Integer sid = fd.getSid();
//            Station station = stationService.getById(sid);
//            fd.setStation(station);

        });
        long count = favoritedetailsService.count();
        return R.ok().setData(list).setCount(count);
    }

    @Operation(summary = "收藏详情查询")
    @GetMapping("/favoritedetails/details/{id}")
    public R details(@PathVariable Integer id){

        QueryWrapper<Favoritedetails> query = Wrappers.query(Favoritedetails.class);
        query.eq("listid",id);
        List<Favoritedetails> list = favoritedetailsService.list(query);
        List<Station> stations = new ArrayList<>();
        list.forEach(fd->{
            Integer sid = fd.getSid();
            Station station = stationService.getById(sid);
            stations.add(station);
        });
        return R.ok().setData(stations);
    }
}
